<?php


/*/=====================================================//
|
| Name  : Hasyim Alkhawarizmi
| Email : hasim319@gmail.com
| Last Updated : 19-05-2014
|
//=====================================================/*/

class Mypaging {

    var $items_per_page;
    var $items_total;
    var $current_page;
    var $num_pages;
    var $mid_range;
    var $low;
    var $limit;
    var $return;
    var $default_ipp;
    var $querystring;
    var $ipp_array;
    var $function_load;
    var $post_page;
    var $post_ipp;

    function Mypaging() {
        $this->current_page = 1;
        $this->mid_range = 7;
        $this->ipp_array = array(10, 25);
    }

    function paginate() {
        $this->items_per_page = ($this->post_ipp != "") ? (int) $this->post_ipp : $this->default_ipp;
        if (!isset($this->default_ipp)) {
            $this->default_ipp = 25;
        }
        if ($this->post_ipp == 'All') {
            $this->num_pages = 1;
        } else {
            if (!is_numeric($this->items_per_page) OR $this->items_per_page <= 0) {
                $this->items_per_page = $this->default_ipp;
            }
            $this->num_pages = ceil($this->items_total / $this->items_per_page);
        }
        $this->current_page = ($this->post_page != "") ? (int) $this->post_page : 1; // must be numeric > 0
        $prev_page = $this->current_page - 1;
        $next_page = $this->current_page + 1;

        if ($this->num_pages > 10) {
            $this->return = ($this->current_page > 1 And $this->items_total >= 10) ? "<a onclick=\"$this->function_load($prev_page, $this->items_per_page)\" class=\"btn btn-default\" >&laquo; Previous</a> " : "<span class=\"btn btn-primary\" href=\"#\">&laquo; Previous</span> ";

            $this->start_range = $this->current_page - floor($this->mid_range / 2);
            $this->end_range = $this->current_page + floor($this->mid_range / 2);

            if ($this->start_range <= 0) {
                $this->end_range += abs($this->start_range) + 1;
                $this->start_range = 1;
            }
            if ($this->end_range > $this->num_pages) {
                $this->start_range -= $this->end_range - $this->num_pages;
                $this->end_range = $this->num_pages;
            }
            $this->range = range($this->start_range, $this->end_range);

            for ($i = 1; $i <= $this->num_pages; $i++) {
                if ($this->range[0] > 2 And $i == $this->range[0]) {
                    $this->return .= " ... ";
                }
                if (($i === 1) || ($i == $this->num_pages) || (in_array($i, $this->range))) {
                    $this->return .= ($i == $this->current_page And $this->post_page != 'All') ? "<a  title=\"Go to page $i of $this->num_pages\" class=\"btn btn-primary\" href=\"#\">$i</a> " : "<a onclick=\"$this->function_load($i, $this->items_per_page)\" class=\"btn btn-default\" title=\"Go to page $i of $this->num_pages\" href=\"#\">$i</a> ";
                }
                if (($this->range[$this->mid_range - 1] < $this->num_pages - 1) && ($i == $this->range[$this->mid_range - 1])) {
                    $this->return .= " ... ";
                }
            }
            $this->return .= (($this->current_page < $this->num_pages And $this->items_total >= 10) And ($this->post_page != 'All') And $this->current_page > 0) ? "<a onclick=\"$this->function_load($next_page, $this->items_per_page)\" class=\"btn btn-default\" href=\"#\">Next &raquo;</a>\n" : "<span class=\"btn btn-primary\" href=\"#\">&raquo; Next</span>\n";
        } else {
            for ($i = 1; $i <= $this->num_pages; $i++) {
                $this->return .= ($i == $this->current_page) ? "<a class=\"btn btn-primary\" href=\"#\">$i</a> " : "<a onclick=\"$this->function_load($i, $this->items_per_page)\" class=\"btn btn-default\" >$i</a> ";
            }
        }
        $this->low = ($this->current_page <= 0) ? 0 : ($this->current_page - 1) * $this->items_per_page;
        if ($this->current_page <= 0) {
            $this->items_per_page = 0;
        }

        $start = $this->low + 1;
        $end = $this->low + $this->items_per_page;

        $this->limit = ($this->post_ipp == 'All') ? "" : " where recnum >= $start and recnum <= $end ";
    }

    function display_items_per_page() {
        $items = '';
        if (!isset($this->post_ipp)) {
            $this->items_per_page = $this->default_ipp;
        }
        foreach ($this->ipp_array as $ipp_opt) {
            $items .= ($ipp_opt == $this->items_per_page) ? "<option selected value=\"$ipp_opt\">$ipp_opt</option>\n" : "<option value=\"$ipp_opt\">$ipp_opt</option>\n";
        }
        return "<span class=\"paginate\">Total : <font style='color:blue;text-decoration:underline;'>" . $this->items_total . ".</font> Items per page:</span><select id=\"ipp_val\" class=\"paginate\" onchange=\"$this->function_load(1,this[this.selectedIndex].value)\">$items</select>\n";
    }

    function display_jump_menu() {
        $option = "";
        for ($i = 1; $i <= $this->num_pages; $i++) {
            $option .= ($i == $this->current_page) ? "<option value=\"$i\" selected>$i</option>\n" : "<option value=\"$i\">$i</option>\n";
        }
        return "<span class=\"paginate\">Page:</span><select id=\"page_val\" class=\"paginate\" onchange=\"$this->function_load(this[this.selectedIndex].value,$this->items_per_page)\">$option</select>\n";
    }

    function display_pages() {
        return $this->return;
    }

}
